#include "stdafx.h"
#include "MainView.h"
#include "layout.h"
#include "LayoutHorizontal.h"
#include "LayoutVertical.h"
#include "ConnectTipView.h"
#include "TextView.h"
#include "RichEditView.h"
#include "WidgetWin.h"
#include "CalcAnimation.h"
#include "Edit.h"
#include "TreeView.h"
#include "autotest/Manage.h"
#include "res.h"

MainView::MainView()
{
	// animation_ = std::shared_ptr<CalcAnimation>(new CalcAnimation(0, 10));

	// animation_->setDelegate(this);
	// animation_->start(this);
}


MainView::~MainView()
{
}

bool MainView::init()
{	
	if (getLayout() == NULL)
	{
		Layout* layout = new LayoutVertical();
		setLayout(layout);
		layout->addFixRow(100); // controller bar
		{
			Layout* layoutStart = new LayoutHorizontal();
			layoutStart->addColumn(100);
			{
				Layout* layoutContent = new LayoutVertical();
				layoutContent->addRow(50);
				layoutContent->addFixRow(25);
				{
					Layout* layoutDCUInfo = new LayoutHorizontal();
					layoutDCUInfo->addColumn(25);
					TextView* text = new TextView(this);
					text->setID(ID_TEXT_MODEL_1);
					text->setText(std::wstring(L"MODEL:---"));
					layoutDCUInfo->add(0, text);
					layoutDCUInfo->addColumn(25);
					text = new TextView(this);
					text->setID(ID_TEXT_TYPE_1);
					text->setText(std::wstring(L"TYPE:---"));
					layoutDCUInfo->add(1, text);
					layoutDCUInfo->addColumn(25);
					text = new TextView(this);
					text->setID(ID_TEXT_REG_1);
					text->setText(std::wstring(L"REGION:---"));

					layoutDCUInfo->add(2, text);
					layoutDCUInfo->addColumn(25);
					text = new TextView(this);
					text->setID(ID_TEXT_COM_1);
					text->setText(std::wstring(L"COM:---"));

					layoutDCUInfo->add(3, text);

					layoutContent->add(1, layoutDCUInfo);
				}

				layoutContent->addFixRow(10);
				layoutContent->addFixRow(25);
				{
					Layout* layoutMEUInfo = new LayoutHorizontal();
					layoutMEUInfo->addColumn(25);
					TextView* text = new TextView(this);
					text->setID(ID_TEXT_MODEL_2);
					text->setText(std::wstring(L"MODEL:---"));

					layoutMEUInfo->add(0, text);
					layoutMEUInfo->addColumn(25);
					text = new TextView(this);
					text->setID(ID_TEXT_TYPE_2);
					text->setText(std::wstring(L"TYPE:---"));

					layoutMEUInfo->add(1, text);
					layoutMEUInfo->addColumn(25);
					text = new TextView(this);
					text->setID(ID_TEXT_REG_2);
					text->setText(std::wstring(L"REGION:---"));

					layoutMEUInfo->add(2, text);
					layoutMEUInfo->addColumn(25);
					text = new TextView(this);
					text->setID(ID_TEXT_COM_2);
					text->setText(std::wstring(L"COM:---"));

					layoutMEUInfo->add(3, text);

					layoutContent->add(3, layoutMEUInfo);

				}
				layoutContent->addRow(50);

				layoutStart->add(0, layoutContent);
			}
			layoutStart->addFixColumn(80);
			{
				Layout* layoutContent = new LayoutVertical();
				layoutContent->addRow(50);
				layoutContent->addFixRow(30);
				Button* start_button = new Button(this, this);
				start_button->setID(ID_BUTTON_OK);
				start_button->setText(std::wstring(L"Start AutoText"));
				layoutContent->add(1, start_button);
				layoutContent->addRow(50);
				layoutStart->add(1, layoutContent);
			}
			layoutStart->addFixColumn(15);

			layout->add(0, layoutStart);
		}



		layout->addRow(100); // main board

		{
			Layout* layoutContent = new LayoutHorizontal();
			layoutContent->addFixColumn(250);
			layoutContent->addFixColumn(5);
			TreeView* tree_view = new TreeView(this);
			layoutContent->add(0,tree_view);
			tree_view->setID(ID_TREEVIEW_TESTCASE);
			layoutContent->addColumn(100);
			Edit* edit = new Edit(this);
			edit->setID(ID_EDIT_LOG);
			layoutContent->add(2, edit);

			layout->add(1, layoutContent);
		}

		layout->addFixRow(25);
	}

	return true;
}

bool MainView::deinit()
{
	return false;
}

// AnimationDelegate
void MainView::animateStarted(Animation* animation)
{

}

void MainView::animateCompleted(Animation* animation)
{

}

void MainView::animateStep(Animation* animation)
{
	// ATLTRACE("USB Device connected \r\n");
	static int x = Width();
	Layout* layout = getLayout();
	if ( layout && x >= 0 )
	{
		setBolder(x, 0, Width(), Height());
		// layout->setRect(x, 0, Width(), Height());
		// layout->updateLayout();
		updateLayout();
		x -= 5;
		if (x<0)
		{
			x = 0;
		}
	}
}

void MainView::animateCanceled(Animation* animation)
{

}

void MainView::ButtonPressed(Button* sender, MouseEvent& event)
{

	ATLTRACE("[%s][%d][%d]\r\n", __FUNCTION__, __LINE__, sender->getID());

	static bool is_run = false;
	if (sender->getID() == ID_BUTTON_OK)
	{
		Manage::getInstance().goRun(!is_run);
		is_run = !is_run;
	}
}